#include "coconut.h"

Coconut::Coconut() { }

Coconut::Coconut(PlayerType initOwner, int yPos)
{
    initialize(initOwner, yPos);
}

// initializes coconut for constructors
void Coconut::initialize(PlayerType initOwner, int yPos)
{
    int xPos;
    if (initOwner == HOST) {
        xPos = MONKEY_DISTANCE_FROM_BORDER + COCO_X_OFFSET;
    } else {
        xPos = WORLD_WIDTH - MONKEY_DISTANCE_FROM_BORDER - COCO_X_OFFSET - COCO_WIDTH;
    }

    imageIndex = 0;
    image = COCONUT_FRAMES.at(imageIndex);
    rect = QRect(xPos, yPos, COCO_WIDTH, COCO_HEIGHT);
    type = COCONUT;
    isMovable = true;
    isHitable = false;
    destroyLater = false;
    speed = COCO_SPEED.at(_difficulty_) * (initOwner == 0 ? 1 : -1);
    owner = initOwner;
}


// updates the coconut's position in world
void Coconut::updatePosition()
{
    if (isMovable)
    {
        rect.translate(speed, 0);

        if (this->getRight() < 0 || this->getLeft() > WORLD_WIDTH)
        {
            isMovable = false;
            destroyLater = true;
        }

        mObs->movedInModel(objId, rect.x(), rect.y());
    }
}

void Coconut::nextImage()
{
    imageIndex = (imageIndex + 1) % COCONUT_FRAMES.size();
    image = COCONUT_FRAMES.at(imageIndex);
    mObs->changedFrameInModel(objId, image);
}


Coconut::~Coconut()
{
    mObs->destroyedInModel(objId);
}


